<template>
	<view>
		<view class="head">
			<view class="head-num">{{num}}/{{maxNum}}</view>
			<view class="head-top">
				<view class="head-top-in" :style="{'width': (300/maxNum)*num +'rpx'}"></view>
			</view>
			<image src="../../static/images/icon/tips.png" class="icon" @click="showT"></image>
		</view>
		<view class="cont">
			<view class="item" v-for="(item,index) in list" :key="index" @click="toPage(item)">
				<image v-if="item.state==3" :src="item.url" class="img"></image>
				<image v-else src="/static/images/img/aba.jpeg" class="img"></image>
				<view class="item-time">{{item.time | formatDate}}</view>
				<view class="item-bom">
					<view class="text0" v-if="item.state==0">绘制失败</view>
					<view class="text1" v-if="item.state==1">未完成</view>
					<view class="text1" v-if="item.state==2">审核中</view>
					<view class="text2" v-if="item.state==3">已完成</view>
					<image src="../../static/images/icon/del1.png" class="icon" @click.stop="del(item)"></image>
				</view>
			</view>
		</view>
		<image v-if="show" src="../../static/images/icon/shua.png" class="shua" @click="shua"></image>
		<view class="no-box" v-if="list.length==0">
			<image src="../../static/images/icon/icon-no.png" class="icon-no"></image>
			<view class="">暂无内容</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				user_id: uni.getStorageSync('user_id'),
				list: [],
				show: true,
				num: 0,
				maxNum: '',
			}
		},
		computed: {},
		watch: {
			// 状态为 1绘制中 的超过40分钟则为 0失败
			list: {
				handler(val) {
					this.num = val.length
					// console.log('箭头',val)
					val.forEach(e => {
						if (e.state == 1 | e.state == 2) {
							console.log(e)
							let time = this.getDistanceDay(e.time)
							if (time.m > 40) {
								console.log(e._id)
								const db = wx.cloud.database()
								db.collection('order_ai').doc(e._id).update({
									data: {
										state: 0 //超时失败
									}
								}).then(() => {
									this.getList()
								})
							}
						}
					})
				},
				deep: true
			}
		},
		methods: {
			async getOpen() {
				let db = wx.cloud.database()
				let info = await db.collection('config').where({
					name: "config"
				}).get()
				let role = uni.getStorageSync('userInfo').role
				if (role == 1) {
					this.maxNum = info.data[0].roleNum.r1
				} else if (role == 2) {
					this.maxNum = info.data[0].roleNum.r2
				}else{
					this.maxNum = info.data[0].roleNum.r2
				}
				console.log(role, info.data[0].roleNum)
				// this.open = info.data[0].open
				// this.info = info.data[0]
			},
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				let info = uni.getStorageSync('userInfo')
				const db = wx.cloud.database()
				db.collection('order_ai').where({
					user_id: this.user_id
				}).orderBy('time', 'desc').get().then(res => {
					let infoObj = res.data
					let list = []
					let delNum = parseInt(this.maxNum)
					if (infoObj.length > delNum) {
						list = infoObj.slice(0, delNum)
						let listDel = infoObj.slice(delNum)
						this.delList(listDel)
					} else {
						list = infoObj
					}

					this.list = list
					uni.hideLoading()
				})
			},
			toPage(e) {
				uni.navigateTo({
					url: '/sub-diy/aiDetail/aiDetail?_id=' + e._id
				})
			},
			del(e) {
				console.log(e)
				let that = this
				uni.showModal({
					title: '提示',
					content: '确认删除本条绘画',
					success: function(res) {
						if (res.confirm) {
							wx.cloud.callFunction({
								name: 'orderDel',
								data: {
									_id: e._id,
									url: e.url
								}
							}).then(() => {
								that.getList()
							})
						} else if (res.cancel) {}
					}
				})

			},
			delList(list) {
				let that = this
				list.forEach(e => {
					console.log(e)
					wx.cloud.callFunction({
						name: 'orderDel',
						data: {
							_id: e._id,
							url: e.url
						}
					})
				})
			},
			shua() {
				this.getList()
				this.show = false
				setTimeout(() => {
					this.show = true
				}, 5000)
			},
			getDistanceDay(time) {
				var stime = new Date().getTime();
				var etime = time.toString().length == 8 ? parseInt(time) * 1000 : parseInt(time)
				var etime = time; // 传入的时间戳是8位则需要乘1000
				var usedTime = stime - etime; //两个时间戳相差的毫秒数
				//计算出小时数
				var leave = usedTime % (24 * 3600 * 1000);
				var hours = (Math.floor(leave / (3600 * 1000)))
				//计算相差分钟数
				var minutes = (Math.floor(leave / (60 * 1000)))
				// 计算出秒

				return {
					h: hours,
					m: minutes,
				}
			},
			showT() {
				uni.showModal({
					title: '提示',
					content: '普通用户可以存储10张图，超出会删除最早一条',
					success: function(res) {}
				})
			},

		},
		onLoad(options) {
			this.getOpen()
			this.getList()
			// const db = wx.cloud.database()
			// let traceId = await db.collection('order_ai').where({
			// 	traceId: "636fd051-4b63fc88-338d7f0e"
			// }).get()
			// console.log("111",traceId.data[0]._id)
		},
		onShow() {},
		onReady() {},
		onPullDownRefresh() {},
		onReachBottom() {},
		onShareAppMessage() {},
		onPageScroll() {},

	}
</script>

<style lang="scss" scoped>
	.head {
		background-color: #fff;
		height: 80rpx;
		display: flex;
		align-items: center;

		.head-top {
			margin-left: 30rpx;
			width: 300rpx;
			height: 30rpx;
			background-color: #ccc;
			color: red;

			.head-top-in {
				height: 30rpx;
				background: linear-gradient(#00AEEC, #1296db);
			}

		}

		.head-num {
			font-size: 28rpx;
			margin-left: 20rpx;
		}

		.icon {
			width: 40rpx;
			height: 40rpx;
			margin-left: 20rpx;
		}
	}

	.cont {
		width: 690rpx;
		margin-left: 30rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding-bottom: 50rpx;

		.item {
			// width: 320rpx;
			background-color: #fff;
			border-radius: 10rpx;
			margin-top: 20rpx;
			text-align: center;
			padding: 10rpx;

			.img {
				width: 300rpx;
				height: 400rpx;
			}

			.item-time {
				font-size: 20rpx;
				margin-top: 10rpx;
			}

			.item-bom {

				.text0 {
					color: red;
				}

				.text1 {
					color: #8a8a8a;
				}

				.text2 {
					color: #18D46C;
				}

				position: relative;

				.icon {
					width: 40rpx;
					height: 40rpx;
					position: absolute;
					right: 20rpx;
					top: 0rpx;
				}
			}


		}
	}

	.shua {
		position: fixed;
		right: 60rpx;
		bottom: 160rpx;
		width: 80rpx;
		height: 80rpx;
	}

	.no-box {
		width: 100%;
		text-align: center;
		margin-top: 200rpx;
		color: #8a8a8a;

		.icon-no {
			width: 300rpx;
			height: 200rpx;
			margin-bottom: 30rpx;
		}
	}
</style>
